<script lang="ts" setup>
import { forgetPassword } from '@/api/user'

import XButton from '@/components/Form/XButton.vue'
import XPasswordInput from '@/components/Form/XPasswordInput.vue'
import { delayNavigateTo } from '@/utils/navigator'

const { passwords, formData, clearInfo } = useForgetStore()

const { base, success } = useToast()

// 确认按钮
async function onClick() {
  if (passwords.password !== passwords.rePassword) {
    base('两次密码不一致！')
    return
  }
  const { code, msg } = await forgetPassword({
    code: formData.code,
    phone: formData.account,
    password: passwords.password
  })
  if (code !== 0) {
    base(msg || '修改失败，请稍后重试！')
  } else {
    clearInfo()
    success(msg || '修改成功！')
    delayNavigateTo('/pages/home/index')
  }
}
</script>

<template>
  <view class="p-11.5">
    <view>
      <XPasswordInput v-model="passwords.password" placeholder="请输入新密码" />
    </view>
    <view m="t-5 b-6.25">
      <XPasswordInput v-model="passwords.rePassword" placeholder="请确认新密码" />
    </view>
    <XButton :form-data="passwords" @click="onClick()">
      完成
    </XButton>
  </view>
</template>
